using System;

using octalforty.Waffle.ComponentModel.Security;

using Spring.Globalization;

namespace octalforty.Waffle.Web.Engine.Globalization.Formatters
{
    /// <summary>
    /// An <see cref="IFormatter"/>, which can <see cref="IFormatter.Parse"/> string representation
    /// of a password to its' byte representation.
    /// </summary>
    public sealed class PasswordFormatter : IFormatter, ISecurityServiceDependency
    {
        #region Private Member Variables
        private ISecurityService securityService;
        #endregion

        /// <summary>
        /// Initializes a new instance of <see cref="PasswordFormatter"/> class.
        /// </summary>
        /// <param name="securityService"></param>
        public PasswordFormatter(ISecurityService securityService)
        {
            this.securityService = securityService;
        }

        #region IFormatter Members
        /// <summary>
        /// Formats the specified value.
        /// </summary>
        /// <param name="value">The value to format.</param>
        /// <returns>
        /// Formatted <paramref name="value" />.
        /// </returns>
        public string Format(object value)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// Parses the specified value.
        /// </summary>
        /// <param name="value">The value to parse.</param>
        /// <returns>
        /// Parsed <paramref name="value" />.
        /// </returns>
        public object Parse(string value)
        {
            return SecurityService.GetPasswordHash(value);
        }
        #endregion

        #region ISecurityServiceDependency Members
        /// <summary>
        /// Gets or sets a reference to the <see cref="ISecurityService" />.
        /// </summary>
        public ISecurityService SecurityService
        {
            get { return securityService; }
            set { securityService = value; }
        }
        #endregion
    }
}
